package com.dhf.framework.system.controller;

import java.math.BigInteger;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dhf.framework.base.mybatis.core.page.Pagination;
import com.dhf.framework.base.utils.Coder;
import com.dhf.framework.base.utils.FilesUtil;
import com.dhf.framework.base.utils.UploadUtil;
import com.dhf.framework.common.BaseController;
import com.dhf.framework.system.entity.SysUser;
import com.dhf.framework.system.service.SysUserService;

 /**
 * </b>SysUserController<br>
 * @author HoraceHo
 * @version:1.0
 */
@Controller
@RequestMapping("/system/sysUser")
public class SysUserController extends BaseController{
		private static Logger LOGGER = LoggerFactory.getLogger(SysUserController.class);
	
		private final static String RELATIVEPATH="sysUser";
		
  		@Autowired
  		private SysUserService sysUserService;
  		
  		
  		
  		/**
		  * 
		  * @Title: queryPageList
		  * @Description: 列表页面
		  * @param @param searchEntity
		  * @param @param pagination
		  * @return ModelAndView    返回类型
		  * @throws
		*/
		@RequestMapping(value = "/queryPageList")
		public ModelAndView queryPageList(SysUser sysUser, Pagination<SysUser> pagination) {
			LOGGER.debug(SysUserController.class.getName()+":开始查询分页数据！");
			ModelAndView mv = new ModelAndView( "system/sysUser/sysUserList" );
			pagination = this.sysUserService.queryPageList( sysUser, pagination );
			mv.addObject( "pagination", pagination );
			return mv;
		}
		
		
		/**
		 * 
		 * @Title: toMerge
		 * @Description: 新增或修改页面
		 * @param @param entity
		 * @return ModelAndView    返回类型
		 * @throws
		 */
		@RequestMapping(value = "/toMerge")
		public ModelAndView toMerge(SysUser entity){
			ModelAndView mv = new ModelAndView("system/sysUser/sysUserMerge");
			if(entity.getId() != null){
				SysUser sysUser =  this.sysUserService.get( entity.getId() ) ;
				mv.addObject("sysUser",sysUser);
			}else{
				mv.addObject("sysUser", new SysUser());
			}
			return mv;
		}

		/**
		 * 
		* @Title: doMerge
		* @Description: 新增或修改数据 
		* @param @param request
		* @param @param entity
		* @return ModelAndView    返回类型
		* @throws Exception
		 */
		@RequestMapping(value = "/doMerge")
		public ModelAndView doMerge(HttpServletRequest request, SysUser entity,@RequestParam(value="fileUrl",required=false)MultipartFile file,RedirectAttributes redirectAttributes) throws Exception {
			if(file != null && file.getSize() > 0){
	    		String fileName=UploadUtil.doUpload(RELATIVEPATH, file,request);
		    	entity.setPhoto(fileName);
			}
			
			String pword=entity.getPassword();
			if(StringUtils.isNotEmpty(pword)){
				String password=new BigInteger(Coder.encryptHMAC(entity.getPassword().getBytes(), entity.getPassword())).toString(16);
		        entity.setPassword(password);
			}
			
			SysUser sysUser=getUser();   

			if (entity.getId()!=null) {
				LOGGER.debug(SysUserController.class.getName()+":开始更新数据！");
				this.addMessage(redirectAttributes, "修改数据成功");
				entity.setUpdateBy(sysUser.getName());
				entity.setUpdateDate(new Date());
				this.sysUserService.update( entity );
			} else {
				LOGGER.debug(SysUserController.class.getName()+":开始新增数据！");
				entity.setCreateBy(sysUser.getName());
				entity.setCreateDate(new Date());
				this.sysUserService.add( entity );
				this.addMessage(redirectAttributes, "新增据成功");
			}
			return new ModelAndView( "redirect:/system/sysUser/queryPageList.do" );
		}

		/**
		 * 
		 * @Title: delete
		 * @Description:删除数据
		 * @param @param id
		 * @return ModelAndView    返回类型
		 * @throws
		 */
		@RequestMapping(value = "/delete")
		public ModelAndView delete(Long id,RedirectAttributes redirectAttributes) {
			LOGGER.debug(SysUserController.class.getName()+":开始删除数据！");
	        this.sysUserService.delete( id );
	        this.addMessage(redirectAttributes, "删除数据成功");
	        return new ModelAndView( "redirect:/system/sysUser/queryPageList.do" );
		}
		
		
		@RequestMapping(value = "/getImage")
		public static void getImage(String fileName,HttpServletRequest request, HttpServletResponse response){
			FilesUtil.getFile(RELATIVEPATH,fileName,request, response);
		}
}

